package org.jdsm.flow.gxt.users.client.widgets.role;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.AttachEvent;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.data.client.loader.HttpProxy;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.loader.LoadResultListStoreBinding;
import com.sencha.gxt.data.shared.loader.PagingLoadConfig;
import com.sencha.gxt.data.shared.loader.PagingLoader;
import com.sencha.gxt.data.shared.writer.JsonWriter;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;
import com.sencha.gxt.widget.core.client.grid.ColumnModel;
import com.sencha.gxt.widget.core.client.grid.Grid;
import com.sencha.gxt.widget.core.client.info.Info;
import org.jdsm.flow.gxt.users.client.UserAutoBeanFactory;

import java.util.ArrayList;
import java.util.List;

import static org.jdsm.flow.gxt.users.client.UserAutoBeanFactory.*;

/**
 * Created by IntelliJ IDEA.
 * User: mikom
 * Date: 7/5/12
 * Time: 6:05 PM
 */
public class RolesGrid implements IsWidget {

    PagingLoader<PagingLoadConfig, RolePagingLoadResult> loader;
    private Grid<UserAutoBeanFactory.Role> grid;

    private String rolesPath = "data/roles.jsp";
    private String saveOrUpdateRolePath;

    public RolesGrid(String rolesPath, String saveOrUpdateRolePath) {
        this.rolesPath = rolesPath;
        this.saveOrUpdateRolePath = saveOrUpdateRolePath;
    }

    public  Grid<UserAutoBeanFactory.Role> getGrid(){
        return grid;
    }

    @Override
    public Widget asWidget() {
        UserAutoBeanFactory factory = GWT.create(UserAutoBeanFactory.class);

        RoleDataRecordJsonReader reader =
                new RoleDataRecordJsonReader(factory);


        RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, rolesPath);
        HttpProxy<PagingLoadConfig> proxy = new HttpProxy<PagingLoadConfig>(builder);
        JsonWriter<PagingLoadConfig> writer = new JsonWriter<PagingLoadConfig>(factory, PagingLoadConfig.class);
        proxy.setWriter(writer);

        loader = new PagingLoader<PagingLoadConfig, RolePagingLoadResult>(proxy, reader);
        loader.useLoadConfig(factory.pagingLoadConfig().as());
        loader.setLimit(10);
        loader.setRemoteSort(true);
        UserAutoBeanFactory.RoleProperties props = GWT.create(UserAutoBeanFactory.RoleProperties.class);

        ListStore<UserAutoBeanFactory.Role> store = new ListStore<UserAutoBeanFactory.Role>(props.key());
        loader.addLoadHandler(new LoadResultListStoreBinding<PagingLoadConfig,
                UserAutoBeanFactory.Role,
                RolePagingLoadResult>(store));

        final ColumnConfig<UserAutoBeanFactory.Role, String> columnName = new ColumnConfig<UserAutoBeanFactory.Role, String>(props.name(), 200, "Name");
        final ColumnConfig<UserAutoBeanFactory.Role, Integer> columnPermission = new ColumnConfig<UserAutoBeanFactory.Role, Integer>(props.permission(), 200, "permission");

        List<ColumnConfig<UserAutoBeanFactory.Role, ?>> l = new ArrayList<ColumnConfig<UserAutoBeanFactory.Role,?>>();
        l.add(columnName);
        l.add(columnPermission);
        ColumnModel<UserAutoBeanFactory.Role> cm = new ColumnModel<UserAutoBeanFactory.Role>(l);

       grid = new Grid<UserAutoBeanFactory.Role>(store, cm);
        grid.getView().setForceFit(true);
        grid.setLoader(loader);
        grid.setLoadMask(true);
        grid.setBorders(true);
        grid.getView().setEmptyText("No result found");

        /*grid.addCellDoubleClickHandler(new CellDoubleClickEvent.CellDoubleClickHandler() {
            @Override
            public void onCellClick(CellDoubleClickEvent event) {
                *//*final GridRowEditing<Role> editing = new GridRowEditing<UserAutoBeanFactory.Role>(grid);
                editing.startEditing(new Grid.GridCell(0, 0));*//*

            }
        });*/

        EditingForRoles er = new EditingForRoles(grid, columnName, columnPermission, saveOrUpdateRolePath);
                        er.init();


        grid.addAttachHandler(new AttachEvent.Handler() {
            @Override
            public void onAttachOrDetach(AttachEvent event) {
                Timer t = new Timer(){
                    @Override
                    public void run() {
                        loader.load();
                    }
                };
                t.schedule(1000);
            }
        });



//        grid.getSelectionModel().addL

        return grid;
    }

    public PagingLoader<PagingLoadConfig, RolePagingLoadResult> getLoader() {
        return loader;
    }


    public boolean newRole(){
        return false;
    }

    public boolean removeRole(){
        return false;
    }

    public boolean saveChanges(){
        return false;
    }

    public void saveAction(Role selectedRole) {

    }

    public void saveData() {


    }

    public void removeAction(Role selectedRole) {
        Info.display("MessageBox", selectedRole.getName());
        PagingLoadConfig config = loader.getLastLoadConfig();
        loader.load(config);
    }

    public void removeData() {


    }
}
